package org.reliablesource.prism.gef.editor;

import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.IPrismUnitAttribute;
import org.reliablesource.prism.gef.editor.model.PrismGEFAdapterFactory;

/**
 * The main plugin class to be used in the desktop.
 */
public class PrismEditorPlugin extends AbstractUIPlugin {

	// The shared instance.
	private static PrismEditorPlugin plugin;

	private PrismGEFAdapterFactory adapterFactory;

	private IAdapterManager adapterManager;

	// private IStatusLineManager statusLine;
	/**
	 * The constructor.
	 */
	public PrismEditorPlugin() {
		plugin = this;
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(final BundleContext context) throws Exception {
		super.start(context);
		adapterFactory = PrismGEFAdapterFactory.getInstance();
		adapterManager = Platform.getAdapterManager();
		adapterManager.registerAdapters(adapterFactory, IPrismTutorial.class);
		adapterManager.registerAdapters(adapterFactory, IPrismScreen.class);
		adapterManager.registerAdapters(adapterFactory,
				IPrismUnitAttribute.class);

	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(final BundleContext context) throws Exception {
		adapterManager.unregisterAdapters(adapterFactory, IPrismTutorial.class);
		adapterManager.unregisterAdapters(adapterFactory, IPrismScreen.class);
		adapterManager.unregisterAdapters(adapterFactory,
				IPrismUnitAttribute.class);
		adapterManager = null;

		super.stop(context);
		plugin = null;
		super.stop(context);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 */
	public static PrismEditorPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path.
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin(
				"org.reliablesource.prism.gef", path);
	}

	// private static ImageRegistry image_registry;
	//
	// public static URL newURL(String url_name) {
	// try {
	// return new URL(url_name);
	// } catch (MalformedURLException e) {
	// throw new RuntimeException("Malformed URL " + url_name, e);
	// }
	// }
	//
	// public ImageRegistry getImageRegistry() {
	// if (image_registry == null) {
	// image_registry = new ImageRegistry();
	//			
	// image_registry.put(
	// "icons/newConnection.gif",
	// ImageDescriptor.createFromURL(newURL("file:icons/newConnection.gif")));
	// image_registry.put(
	// "icons/newModel.gif",
	// ImageDescriptor.createFromURL(newURL("file:icons/newModel.gif")));
	// image_registry.put(
	// "icons/pointer.GIF",
	// ImageDescriptor.createFromURL(newURL("file:icons/pointer.GIF")));
	// image_registry.put(
	// "icons/rectangle16.gif",
	// ImageDescriptor.createFromURL(newURL("file:icons/rectangle16.gif")));
	// image_registry.put(
	// "icons/rectangle24.gif",
	// ImageDescriptor.createFromURL(newURL("file:icons/rectangle24.gif")));

	// File[] images = getImagesFromStorage();
	//
	//			
	// for (int i = 0; i < images.length; i++) {
	// if (images[i].toString().split(".").length > 0)
	// try {
	// image_registry.put(images[i].toString(),
	// ImageDescriptor
	// .createFromURL(images[i].toURL()));
	// } catch (MalformedURLException e) {
	// e.printStackTrace();
	// }
	// }

	// }
	// return image_registry;
	// }

//	private File[] getImagesFromStorage() {
//		File[] images;
//		File folder = null;
//		try {
//			folder = new File(new URL("file:icons").toURI());
//		} catch (MalformedURLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (URISyntaxException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		System.err.println(folder.getAbsoluteFile());
//		images = new File[folder.listFiles().length];
//		images = folder.listFiles();
//		return images;
//	}
}
//}
